<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}
/**
 * MchangesClass订单操作类
 * add()
 * del()
 * state()
 * getdata()
 *
 */
class AccountChargeClass {
	var $db;
	var $table;
	var $tablepre;
	function __construct(){
		//设置全局变量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."accountcharge";
	}
	function AccountChargeClass() {
		$this->__construct();
	}
	/**
	 * 添加会员充值记录
	 *
	 * @param array $sarray
	 * @return OK/提示
	 */
	function add($sarray){
		$timestamp=time();
		if(!empty($sarray['uid']) && !empty($sarray['uname']) && !empty($sarray['money'])){
			$sarray['sn']=$this->makesn($sarray['uid']);
			$sarray['addtime']=$timestamp;
			$sarray['state']=0;
			$sid=inserttable($this->table, $sarray,1);
			if(is_numeric($sid))return $sarray['sn'];
			else return "插入数据库出错";
		}return "缺少参数";
	}
	/**
	 * 编辑信息
	 *
	 * @param int $sn
	 * @param string $sarray
	 * @return OK/提示
	 */
	function edit($sn,$sarray){
		if(is_array($sarray) && !empty($sn)){
				//更新数据库操作
				$wheresqlarr=" sn='{$sn}'";
				updatetable($this->table,$sarray, $wheresqlarr);
				return "OK";
		}return "参数错误";
	}
	/**
	 * 随机生成订单编号
	 *
	 * @param int $gid
	 * @param int $uid
	 * @return string
	 */
	function makesn($uid=0){
		$autokeys=randomkeys(3,"123");//随机5位数字
		$timestr=date('YmdHis');
		$sn=$uid.$timestr.$autokeys;
		return $sn;
	}
	
	/**
	 * 修改付款状态 
	 * 参数1时候 已付款 0未付款
	 * @param int $oid
	 * @param int $num
	 * @return true/false
	 */
	function edit_money_state($sn,$money=0){
		if(GetNum($sn) && GetNum($money)){
			$value=$this->getone($sn);
			if(empty($value))return "相关数据不存在";
			if($value['state']==1)return true;
			$this->db->query("update `$this->table` SET state = 1 WHERE sn='{$sn}'");//修改付款状态
			include_once(INC_PATH."/member.class.php");
			$m=new memberclass();
			$note="用户：".$value["uname"]."充值";
			if($m->moneyedit($value["uname"],$money,$note)){
				return true;
			}else {
				$this->db->query("update `$this->table` SET state = 0 WHERE sn='{$sn}'");//修改付款状态
				return "充值操作失败";
			}
		}else return "数据类型错误";
	}
	
	/**
	 * 清空未付费记录
	 *
	 * @return true/false
	 */
	function clear($date=0){
		$oldtime=time()- $date*24*3600;
		$wherestr="state=0 and addtime < $oldtime";
		$this->db->query("delete from {$this->table} where $wherestr");	
		return true;	
	}
	/**
	 * 获取一条信息
	 *
	 * @param unknown_type $oid
	 * @return unknown
	 */
	function getone($sn){
		if (!empty($sn)) {
			$gpdata=$this->getdata(1,"sn='{$sn}'");
			$value=$gpdata[0];
		}
		return $value;
	}
	/**
	 * 获取数据数组
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby="")
	{
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by aid desc";

		$sql="select * from {$this->table}{$where}{$orderby}{$limit}";
		$query =$this->db->query($sql);
		while($value = $this->db->fetch_array($query)) {
			//此处可进行数据整理
			$tempdata[]=$value;
		}
		return $tempdata;
	}
	function getcount($where=""){
		if(!empty($where))$where=" where $where";
		$count= $this->db->result_first("Select count(*) From `$this->table` $where");
		return $count;
	
	}
}
?>